<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | EmailHelper.php v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : rahul
// | Date : 2018/11/19
// +-------------------------------------------------------------------------------
namespace app\extend\email;

use PHPMailer\PHPMailer\PHPMailer;

class EmailHelper {

    private static $mail;

    public static function send($to, $name, $subject, $body, $attachment = null) {
        if (self::$mail == null) {
            self::$mail = new PHPMailer();
            $config = config('mail');
            self::$mail->CharSet = $config['charset'];
            self::$mail->IsSMTP();

            self::$mail->SMTPSecure = $config['secure'];
            self::$mail->Host = $config['host'];
            self::$mail->Port = $config['port'];

            self::$mail->SMTPAuth = true;
            self::$mail->Username = $config['username'];
            self::$mail->Password = $config['password'];

            self::$mail->setFrom($config['from'], $config['fromname']);
        }
        self::$mail->Subject = $subject;
        self::$mail->MsgHTML($body);
        self::$mail->AddAddress($to, empty($name) ? $to : $name);
        if (is_array($attachment)) {
            foreach ($attachment as $file) {
                is_file($file) && self::$mail->AddAttachment($file);
            }
        }
        return self::$mail->Send() ? '' : self::$mail->ErrorInfo;
    }
}